package com.qfedu.service.impl;

import com.qfedu.dao.OrdersRepository;
import com.qfedu.pojo.entity.Order;
import com.qfedu.pojo.entity.Orders;
import com.qfedu.pojo.req.OrdersReq;
import com.qfedu.pojo.resp.BaseResp;
import com.qfedu.pojo.resp.OrdersResp;
import com.qfedu.service.OrdersService;
import com.qfedu.utils.UUIDUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author:User
 * @time:2022/9/28
 * @explain:
 */

@Service
public class OrdersServiceImpl implements OrdersService {

   @Autowired
   private OrdersRepository ordersRepository;

    /**
     * 生成订单
     * @param ordersReq
     * @return
     */
    @Override
    public BaseResp addOrders(OrdersReq ordersReq) {
        Orders orders = new Orders();
        //属性赋值
        orders.setOrderId(UUIDUtils.getId());
        orders.setAddress(ordersReq.getAddress());
        orders.setCreateTime(ordersReq.getCreateTime());
        orders.setDisTime(ordersReq.getDisTime());
        orders.setUserId(ordersReq.getUserId());
        orders.setStatus(0);//默认为0意味着未支付
        orders.setOrderItem(ordersReq.getOrderItem());
        orders.setTelephone(ordersReq.getTelephone());
        orders.setUser(ordersReq.getUser());
        orders.setOrdersPrice(ordersReq.getOrdersPrice());

        //添加订单
        ordersRepository.save(orders);
        return new BaseResp().Success();

    }

    /**
     * 修改订单
     * @param ordersReq
     * @return
     */
    @Override
    public BaseResp updateOrders(OrdersReq ordersReq) {
        Orders orders = new Orders();
        //属性赋值
        orders.setOrderId(UUIDUtils.getId());
        orders.setAddress(ordersReq.getAddress());
        orders.setCreateTime(ordersReq.getCreateTime());
        orders.setDisTime(ordersReq.getDisTime());
        orders.setUserId(ordersReq.getUserId());
        orders.setStatus(0);//默认为0意味着未支付
        orders.setTelephone(ordersReq.getTelephone());
        orders.setUser(ordersReq.getUser());
        orders.setOrdersPrice(ordersReq.getOrdersPrice());

        //修改
        ordersRepository.saveAndFlush(orders);
        return new BaseResp().Success();
    }

    /**
     * 查询订单
     * @param ordersReq
     * @return
     */
    @Override
    public BaseResp findByUserId(OrdersReq ordersReq) {
        Orders orders = new Orders();
        orders.setUserId(ordersReq.getUserId());
        //根据用户id查询
        List<Orders> byId = ordersRepository.findByUserId(orders.getUserId());

        return new BaseResp(1,"SUCCESS",byId,null);
    }
}
